package com.assisjrs.jpaandroid.sample.basic;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;

import com.assisjrs.jpaandroid.persistence.EntityManager;
import com.assisjrs.jpaandroid.persistence.ModelDefinition;
import com.assisjrs.jpaandroid.sample.R;

public class ListingActivity extends ListActivity {
	private EntityManager entityManager;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getListView().setCacheColorHint(0x00000000);
		registerForContextMenu(getListView());

		refresh();
	}

	@SuppressWarnings("unchecked")
	private void refresh() {
		final List<Costumer> lista = (List<Costumer>) entityManager
				.createNativeQuery("", Costumer.class).getResultList();
		setListAdapter(new CostumerAdapter(this, lista));
	}

	@Override
	protected void onListItemClick(final ListView l, final View v,
			final int position, final long id) {
		super.onListItemClick(l, v, position, id);

		editar(id);
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_item: {
			final Intent it = new Intent(this, CostumerActivity.class);
			startActivityForResult(it, R.id.new_item);

			return true;
		}
		}

		return false;
	}

	@Override
	public void onCreateContextMenu(final ContextMenu menu, final View v,
			final ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_context, menu);
	}

	@Override
	public boolean onContextItemSelected(final MenuItem item) {
		if (item.getItemId() == R.id.remove) {
			final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
					.getMenuInfo();

			final Costumer entity = new Costumer();
			entity.setId(info.id);

			entityManager.remove(entity);
			refresh();
			return true;
		}

		if (item.getItemId() == R.id.update)
			try {
				final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
						.getMenuInfo();

				editar(info.id);

				return true;
			} catch (final Exception e) {
				return false;
			}

		return super.onContextItemSelected(item);
	}

	@Override
	protected void onActivityResult(final int requestCode,
			final int resultCode, final Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == R.id.new_item || requestCode == R.id.update) {
			refresh();
		}
	}

	private void editar(final Long id) {
		final Intent it = new Intent(this, CostumerActivity.class);
		it.putExtra(ModelDefinition.ID_NAME, id);
		startActivityForResult(it, R.id.update);
	}
}
